package org.charles.zhifou.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.charles.zhifou.common.response.ResponseCodeEnum;
import org.charles.zhifou.enums.AppTypeEnum;
import org.charles.zhifou.enums.ReviewStatusEnum;
import org.charles.zhifou.enums.ScoringStrategyEnum;
import org.charles.zhifou.enums.UserRoleEnum;
import org.charles.zhifou.common.exception.BusinessException;
import org.charles.zhifou.common.exception.ThrowUtils;
import org.charles.zhifou.mapper.AppMapper;
import org.charles.zhifou.model.dto.app.AppQueryRequest;
import org.charles.zhifou.model.entity.App;
import org.charles.zhifou.model.entity.User;
import org.charles.zhifou.model.vo.AppVO;
import org.charles.zhifou.service.AppService;
import org.charles.zhifou.service.UserService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author charles
* @description 针对表【app(应用)】的数据库操作Service实现
* @createDate 2024-08-06 09:11:36
*/
@Service
@Slf4j
public class AppServiceImpl extends ServiceImpl<AppMapper, App>
    implements AppService {

    @Resource
    private UserService userService;

    @Resource
    private AppMapper appMapper;

    @Override
    public boolean deleteAppById(Long id) {
        // 判断应用是否存在
        App appToDelete = this.getById(id);
        ThrowUtils.throwIf(appToDelete == null, ResponseCodeEnum.NOT_FOUND_ERROR, "要删除的 App 不存在");

        // 仅本人或管理员可删除
        User loginUser = userService.getLoginUser();
        if (!appToDelete.getUserId().equals(loginUser.getId()) && !UserRoleEnum.ADMIN.equals(loginUser.getUserRole())) {
            throw new BusinessException(ResponseCodeEnum.NO_AUTH_ERROR);
        }

        // 操作数据库
        return this.removeById(id);
    }

    @Override
    public boolean editAppById(App app) {
        // 判断应用是否存在
        App appToEdit = this.getById(app.getId());
        ThrowUtils.throwIf(appToEdit == null, ResponseCodeEnum.NOT_FOUND_ERROR, "要编辑的 App 不存在");

        // 仅本人或管理员可更新
        User loginUser = userService.getLoginUser();
        if (!appToEdit.getUserId().equals(loginUser.getId()) && !UserRoleEnum.ADMIN.equals(loginUser.getUserRole())) {
            throw new BusinessException(ResponseCodeEnum.NO_AUTH_ERROR);
        }

        // 更新规则: 根据 id 更新, 应用名等 5 个字段不为空时更新, 重置审核相关 4 个字段
        Long id = app.getId();
        String appName = app.getAppName();
        String appDesc = app.getAppDesc();
        String appIcon = app.getAppIcon();
        AppTypeEnum appType = app.getAppType();
        ScoringStrategyEnum scoringStrategy = app.getScoringStrategy();
        ReviewStatusEnum reviewStatus = app.getReviewStatus();
        String reviewMessage = app.getReviewMessage();
        Long reviewerId = app.getReviewerId();
        Date reviewTime = app.getReviewTime();

        return this.update(
                new LambdaUpdateWrapper<App>()
                        .set(StringUtils.isNotBlank(appName), App::getAppName, appName)
                        .set(StringUtils.isNotBlank(appDesc), App::getAppDesc, appDesc)
                        .set(StringUtils.isNotBlank(appIcon), App::getAppIcon, appIcon)
                        .set(appType != null, App::getAppType, appType)
                        .set(scoringStrategy != null, App::getScoringStrategy, scoringStrategy)
                        .set(App::getReviewStatus, reviewStatus)
                        .set(App::getReviewMessage, reviewMessage)
                        .set(App::getReviewerId, reviewerId)
                        .set(App::getReviewTime, reviewTime)
                        .eq(App::getId, id)
        );
    }

    @Override
    public boolean reviewAppById(App app) {
        // 判断应用是否存在
        App appToReview = this.getById(app.getId());
        ThrowUtils.throwIf(appToReview == null, ResponseCodeEnum.NOT_FOUND_ERROR, "要审核的 App 不存在");

        // 不可将状态切换为待审核
        ThrowUtils.throwIf(ReviewStatusEnum.REVIEWING.equals(app.getReviewStatus()), ResponseCodeEnum.OPERATION_ERROR, "不可将应用状态切换为待审核");

        // 若已被审核过, 不可再次审核
        ThrowUtils.throwIf(!ReviewStatusEnum.REVIEWING.equals(appToReview.getReviewStatus()), ResponseCodeEnum.OPERATION_ERROR, "该应用已被审核过");

        // 更新
        return this.updateById(app);
    }

    @Override
    public Page<AppVO> listAppVOByPage(Page<AppVO> page, AppQueryRequest appQueryRequest) {
        return appMapper.listAppVOByPage(page, appQueryRequest);
    }

}




